package com.gitee.cirnochat.notification.controller;


import com.gitee.cirnochat.common.annotation.AuthCheck;
import com.gitee.cirnochat.common.model.vo.resp.Result;
import com.gitee.cirnochat.common.utils.RequestUtils;
import com.gitee.cirnochat.notification.model.vo.req.ExpoPushTokenSaveOrUpdateRequest;
import com.gitee.cirnochat.notification.service.ExpoPushTokenService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Tag(name = "Expo推送令牌相关接口", description = "Expo推送令牌相关API")
@RestController
@RequiredArgsConstructor
@RequestMapping("/expopushtoken")
@Validated
public class ExpoPushController {
    private final ExpoPushTokenService expoPushTokenService;

    @Operation(summary = "设置expoPushToken", description = "设置expoPushToken,如果该设备已有expoPushToken,则不会进行设置")
    @ApiResponse(responseCode = "200", description = "设置expoPushToken成功")
    @PostMapping
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Void> setExpoPushToken(@Valid @RequestBody ExpoPushTokenSaveOrUpdateRequest req, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        expoPushTokenService.setExpoPushToken(req.getToken(), req.getDeviceId(), userId);
        return Result.success();
    }


    @Operation(summary = "检查expoPushToken是否设置成功", description = "检查expoPushToken是否设置成功")
    @ApiResponse(responseCode = "200", description = "检查expoPushToken成功",
            content = @Content(
                    mediaType = "application/json",
                    schema = @Schema(implementation = Boolean.class)
            )
    )
    @GetMapping("/check/{deviceId}")
    @Parameter(name = "Authorization", description = "认证信息", required = true, in = ParameterIn.HEADER)
    @Parameter(name = "X-Device-Id", description = "设备ID", required = true , in = ParameterIn.HEADER)
    @AuthCheck
    public Result<Boolean> checkExpoPushToken(@PathVariable String deviceId, HttpServletRequest request) {
        Long userId = RequestUtils.getUserId(request);
        return Result.success(expoPushTokenService.checkExpoPushToken(deviceId,userId));
    }
}
